﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Facebook;
using TeamsPortal.ServiceClient;
using TeamsPortal.Web.Controllers.Helpers;
using TeamsPortal.Web.Extensions.FormsAuthenticationHelper;
using TeamsPortal.Web.Extensions.FormsAuthenticationHelper.Impl;
using TeamsPortal.Web.State;

namespace TeamsPortal.Web.Controllers
{
	public class AuthenticationController : BaseController
	{
        /// <summary>
		/// If the user is not logged in, it ask the user to login using any of the provider.
		/// If the user is logged in and not authorized, it shows a message to the user.
		/// </summary>
		/// <param name="returnUrl"></param>
		/// <param name="group"></param>
		/// <returns>ProviderSelect view result or </returns>
		[HttpGet]
		public ActionResult Login(string returnUrl, UserRole? role)
		{
			if (User != null)
			{
				if (role == null || User.Role >= role)
				{
					return Redirect(returnUrl);
				}
				ViewBag.UserRole = role;
                ViewBag.UserRoleName = UsersServiceClient.GetRoleName(role.Value);
				
                return View("NotAuthorized");
			}
            ViewBag.Template = Template;

			return View("ProviderSelect");
		}

		public ActionResult Logout(string returnUrl)
		{
			Session.User = null;
			IFormsAuthentication formsAuth = new FormsAuthenticationService();
			formsAuth.SignOut();

			return Redirect(returnUrl);
		}
	}
}
